/**
 * service_user_member js file;
 * company:junqitecch
 * author:xuzuiotao
 * 
 */

//test
//
// LOGOUT:32,
//        USER_DETAIL:33,
//        USER_CHANGE_PASSWORD:34,


/**
 * login js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts = require('../consts');
var mail = require('../network/email');
var users= require('../db/users');
var cache=require('../redis/usercache');
var tournote= require('../mongo/tournote');
var qiniu=require('../network/qiniu');
var im =require('../network/im');
var tournote_user=require('../mongo/tournote_user');
var contacts = require('../db/contact');
var member = require('../db/user_member');

async function processList(request,response,callback){
	console.log('processList');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(!data){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	var search = null;
	var pageNO=0;
	var pageSize = 20;
	var totalCount =0;
	
	
	if(param.search){
		search = param.search;
	}

	if(param.pageNO){
		pageNO = param.pageNO;
	}
	
	if(param.pageSize){
		pageSize = param.pageSize;
	}
	
	if(param.totalCount){
		totalCount = param.totalCount;
	}
	
	var wrap = await member.list(request.token,search,pageNO,pageSize,totalCount,null);
	
	//var wrap = await member.list(request.token,search,pageNO,pageSize,totalCount, function(wrap){
		if(wrap===null){

			response.errorCode=-1;
			response.message='更改数据错误，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			response.token=request.token;
			if(callback){
				callback(response);
			}

			return;
			
		}


	return;
	
}


async function processDelete(request,response,callback){

	console.log('processDelete');
	
	var data =request.data;
	if(!request.token){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(!data===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(!param.IDs || !param.IDs.length){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':IDs';
		if(callback){
			callback(response);
		}
		return false;
	}
	
    var ret = await member.del(request.token,param.IDs,null);
	 if(!ret){

			response.errorCode=-1;
			response.message='更改数据错误，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.token=request.token;
			if(callback){
				callback(response);
			}
			return;
		}
}



async function processDetail(request,response,callback){

	console.log('processDetail');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.ID ===null || typeof(param.ID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	users.buyerDetailByID(request.token, param.ID, function(wrap){
		if(wrap===null){

			response.errorCode=-1;
			response.message='非在线支付方式无法在此获取会员详情！';
			if(callback){
				callback(response);
			}
		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			response.token=request.token;
			if(callback){
				callback(response);
			}
			return;
			
		}
		
	});

	return;


}

async function processUpdate(request,response,callback){
	console.log('processUpdate');

	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	
    var ret = await member.update(request.token,param.member,null);
	 if(!ret){

			response.errorCode=-1;
			response.message='更改数据错误，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.token=request.token;
			if(callback){
				callback(response);
			}
			return;
		}

}

async function processImport(request,response,callback){
	console.log('processUpdate');

	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	

	var changevals = [];
    var	i=0;
	if(param.iconURL!==null){
		changevals.icon_url=param.iconURL;
		i++;
	}
	
	if(param.nickName!==null){
		changevals.nick_name=param.nickName;
		i++;

	}
	
	console.log(param.gender);
	
	if(typeof(param.gender)!=='undefined' && param.gender !== null){
		changevals.gender=param.gender;
		i++;

	}
	if(param.mobile!==null){
		changevals.phone_number=param.mobile;
		i++;

	}
	if(param.postalAddress!==null){
		changevals.postal_address=param.postalAddress;
		i++;

	}
	
	if(param.birthDay!==null){
		changevals.birth_date=param.birthDay;
		i++;

	}
	
	if(param.email!==null){
		changevals.address=param.email;
		i++;

	}
	

	if(i<=0){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	

	cache.getByToken(request.token,function(tk,entity){
		if(tk===null || typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		
		entity.qiniuToken=qiniu.uptoken(null);
		users.update(entity.userID,changevals,function(err,entity){
			if(err){
				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}
			}else{
				tournote_user.updateExist(entity,null);
				              
				entity.qiniuToken=qiniu.uptoken(null);
				
				im.getIMToken(entity,function(err){
						cache.refresh(entity,function(tk,user){
							if(tk===null || typeof(tk)==='undefined'){
								response.errorCode=consts.Error.NEED_LOGIN;
								response.message=consts.Message.NEED_LOGIN;
								if(callback){
									callback(response);
								}
							}else{
								response.errorCode = consts.Error.NONE_ERROR;
								response.message= consts.Message.OK_MESSAGE;
								response.data = JSON.stringify(user);
								response.token=tk;
								if(callback){
									callback(response);
								}
							}
						});
						
				});
						
			}
		});
	});

}


module.exports = {
		processUserDetail:processDetail,	
		processUpdate:processUpdate,
		processList:processList,
		processDelete:processDelete,

		processImport:processImport,

		
};

